package com.qf.fmmall.service.impl;

import com.qf.fmmall.dao.UserMapper;
import com.qf.fmmall.entity.User;
import com.qf.fmmall.service.UserService;
import com.qf.fmmall.utils.JwtUtils;
import com.qf.fmmall.utils.MD5Utils;
import com.qf.fmmall.vo.ResStatus;
import com.qf.fmmall.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public  synchronized ResultVo userResgit(String name, String pwd) {

        ResultVo resultVo = new ResultVo();
        //查询用户名是否已经存在
        //根据用户名去数据库查询用户名对应的用户信息
        User condition = new User();
        condition.setUsername(name);
        User user = userMapper.selectOne(condition);
        if (user==null){//插入用户
            User newuser = new User();
            newuser.setUsername(name);
            newuser.setPassword(MD5Utils.md5(pwd));//密码加密
            newuser.setUserRegtime(new Date());
            newuser.setUserModtime(new Date());

            int i = userMapper.insert(newuser);
            if (i==1){
                resultVo.setCode(ResStatus.OK);
                resultVo.setMsg("新增用户成功");
                resultVo.setData(newuser);
            }else {
                resultVo.setCode(ResStatus.OK);
                resultVo.setMsg("数据库操作异常");
            }
        }else {
            resultVo.setCode(ResStatus.NO);
            resultVo.setMsg("当前用户名已经存在，不可以注册");
        }
        return resultVo;
    }


    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVo checkLogin(String name, String pwd) {

        ResultVo resultVo = new ResultVo();
        //根据用户名去数据库查询用户名对应的用户信息
        User condition = new User();
        condition.setUsername(name);
        User user = userMapper.selectOne(condition);
        if (user==null){
            resultVo.setCode(ResStatus.LOGIN_FAILED);
            resultVo.setMsg("用户名不存在");
//            return resultVo;
        }else {
            String dbpassword = user.getPassword();
            String newpassword = MD5Utils.md5(pwd);
            if (dbpassword.equals(newpassword)){
                resultVo.setCode(ResStatus.OK);
                //生成一个token字符串
//                String token = user.getUsername()+"QIANfeng6666";
//                String encode = Base64Utils.encode(token);
                String token = JwtUtils.createtoken(user.getUsername(), user.getUserId() + "");
                resultVo.setMsg(token);
                resultVo.setData(user);
//                return resultVo;
            }else {
                resultVo.setCode(ResStatus.LOGIN_FAILED);
                resultVo.setMsg("密码错误");
            }
        }

        return resultVo;
    }
}
